﻿using AdminBase.Model;
using SqlSugar;

namespace AdminBase.Common.Helpers
{
    /// <summary>
    /// 分页帮助类
    /// </summary>
    public static class PagedHelper
    {
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="queryable">查询对象</param>
        /// <param name="pageBase">分页信息</param>
        /// <returns>包含分页数据的PageInfo对象</returns>
        public static PageInfo GetPageData<T>(ISugarQueryable<T> queryable, PageBase pageBase)
        {
            // 排序
            if (!string.IsNullOrWhiteSpace(pageBase.SortField))
            {
                queryable = pageBase.Ascending
                    ? queryable.OrderBy(pageBase.SortField)
                    : queryable.OrderByPropertyName(pageBase.SortField, OrderByType.Desc);
            }
            int totalCount = 0;
            // 分页查询
            var list = queryable.ToPageList(pageBase.PageIndex, pageBase.PageSize, ref totalCount).ToList();

            PageInfo pageInfo = new PageInfo();
            pageInfo.Data = list;
            pageInfo.Count = totalCount;
            pageInfo.PageIndex = pageBase.PageIndex;
            pageInfo.PageSize = pageBase.PageSize;
            return pageInfo;
        }
    }
}
